package com.elec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elec.domain.R;
import com.elec.mapper.PaymentRecordMapper;
import com.elec.domain.entity.PaymentRecord;
import com.elec.service.PaymentRecordService;
import com.elec.util.ThreadLocalUtil;
import org.springframework.stereotype.Service;

/**
 * 记录用户的缴费信息(PaymentRecord)表服务实现类
 *
 * @author yu
 * @since 2024-07-05 13:42:59
 */
@Service("paymentRecordService")
public class PaymentRecordServiceImpl extends ServiceImpl<PaymentRecordMapper, PaymentRecord> implements PaymentRecordService {

    @Override
    public R add(PaymentRecord paymentRecord) {
        if (save(paymentRecord)){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R delete(Long id) {
        if (removeById(id)){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R update(PaymentRecord paymentRecord) {
        if (updateById(paymentRecord)){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R get(Long id) {
        if (getById(id) != null){
            return R.success(getById(id));
        }
        return R.error("查询失败");
    }

    @Override
    public R getList(Integer pageNum, Integer pageSize, PaymentRecord paymentRecord) {
        LambdaQueryWrapper<PaymentRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (paymentRecord != null){
            if (paymentRecord.getBillId() != null){
                lambdaQueryWrapper.eq(PaymentRecord::getBillId,paymentRecord.getBillId());
            }
            if (paymentRecord.getUserId() != null){
                lambdaQueryWrapper.eq(PaymentRecord::getUserId,paymentRecord.getUserId());
            }
            if (paymentRecord.getPaymentAmount() != null){
                lambdaQueryWrapper.eq(PaymentRecord::getPaymentAmount,paymentRecord.getPaymentAmount());
            }
            if (paymentRecord.getPaymentDate() != null){
                lambdaQueryWrapper.eq(PaymentRecord::getPaymentDate,paymentRecord.getPaymentDate());
            }
        }
        Page<PaymentRecord> page = new Page<>(pageNum,pageSize);
        page(page, lambdaQueryWrapper);
        return R.success(page.getRecords(),page.getTotal());
    }

    @Override
    public R getPayment(Integer pageNum, Integer pageSize, PaymentRecord paymentRecord) {
        Long userId = ThreadLocalUtil.getLocal();
        LambdaQueryWrapper<PaymentRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PaymentRecord::getUserId,userId);
        if (paymentRecord != null){
            if (paymentRecord.getBillId() != null){
                lambdaQueryWrapper.eq(PaymentRecord::getBillId,paymentRecord.getBillId());
            }
            if (paymentRecord.getPaymentAmount() != null){
                lambdaQueryWrapper.eq(PaymentRecord::getPaymentAmount,paymentRecord.getPaymentAmount());
            }
        }
        Page<PaymentRecord> page = new Page<>(pageNum,pageSize);
        page(page, lambdaQueryWrapper);
        return R.success(page.getRecords(),page.getTotal());
    }
}


